#include "head.h"

int recv_file(int net_fd, char *file_all_name)
{   
    int file_fd =open(file_all_name,O_RDWR|O_CREAT,0666);

    // 接收文件大小
    int file_size;
    recv(net_fd, &file_size, sizeof(int), MSG_WAITALL);

    // 高速客户端, 是新文件上传
    self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
    send_mes->cmd=200; // 新文件上传
    send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);

    // 接收文件内容
    int cur_size=0;
    while(cur_size<file_size)
    {
        char buf[1000]={0};
        int ret;
        if(file_size-cur_size < 1000){
            ret =recv(net_fd,buf,file_size - cur_size,MSG_WAITALL);
        }else{
            ret =recv(net_fd,buf,sizeof(buf),MSG_WAITALL);
        }
        if(ret<0)
        {
            close(file_fd);
            free(send_mes);
            return -1;
        }
        write(file_fd,buf,ret);
        cur_size += ret;
    }

    // 回复客户端文件接收完成
    memset(send_mes, 0, sizeof(self_defined_pro_t));
    send_mes->cmd=200; // 上传完毕
    send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);

    free(send_mes);
    close(file_fd);
    return 0;
}

// 有同名文件的处理逻辑
int same_file(int net_fd,char * file_all_path){


    // 接收文件大小
    int file_size;
    recv(net_fd, &file_size, sizeof(int), MSG_WAITALL);

    // 
    int file_fd =open(file_all_path,O_RDWR|O_APPEND);
    struct stat stat_file;
    fstat(file_fd,&stat_file);
    int server_file_size=stat_file.st_size;

    // 比较, 确定是断点续传
    if(server_file_size < file_size){
        // 确定是断点续传
        self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
        send_mes->cmd=210; 
        // 带上文件大小的信息
        send_mes->para_num=1;
        strcat(send_mes->message,"server_file_size=");
        char str[10];
        snprintf(str, sizeof(str), "%d", server_file_size);
        strcat(send_mes->message, str);
        send_mes->message_len=strlen(send_mes->message);

        send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);

        // 接收数据 
        int cur_size=server_file_size;
        while(cur_size<file_size)
        {
            char buf[1000]={0};
            int ret;
            if(file_size-cur_size < 1000){
                ret =recv(net_fd,buf,file_size - cur_size,MSG_WAITALL);
            }else{
                ret =recv(net_fd,buf,sizeof(buf),MSG_WAITALL);
            }
            if(ret<0)
            {
                close(file_fd);
                free(send_mes);
                return -1;
            }
            write(file_fd,buf,ret);
            cur_size += ret;
        }

        close(file_fd);
        // 文件接收完毕
        return 0;

    }else{
        self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
        send_mes->cmd=220; 
        send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
        close(file_fd);
    }

    return 0;
}
int server_upload(int net_fd, self_defined_pro_t * msg)
{  

    char *username=strtok(msg->message,"=");
    username=strtok(NULL,"&");
    char *path=strtok(NULL,"=");
    path=strtok(NULL,"&");
    char *filename=strtok(NULL,"=");
    filename=strtok(NULL,"&");

    // 获得当前路径
    char cwd[200] = {0};
    getcwd(cwd, sizeof(cwd));


    // 拼接路径
    strcat(cwd, "/");
    strcat(cwd, username);
    if(strlen(path) != 1 ){
        strcat(cwd, path);
    }
    strcat(cwd, "/");
    strcat(cwd, filename);

    // 先去判断是否有同名文件, 如果有, 观察是否需要走断点续传逻辑: same_file\函数
    struct dirent *entry;
    char directory[200]={0};
    getcwd(directory, sizeof(directory));
    strcat(directory, "/");
    strcat(directory, username);
    if(strlen(path) != 1 ){
        strcat(directory, path);
    }

    DIR *dir = opendir(directory);

    int flag=0;//判断是否存在同名文件 1为存在(断点续传) 0为不存在直接传
    while ((entry = readdir(dir)) !=NULL) {
        if (strcmp(entry->d_name, filename) == 0) {
            flag = 1; // 找到同名文件
            break; // 跳出循环
        }
    }
    closedir(dir);


    if(flag == 0){
        //接收全新文件
        recv_file(net_fd,cwd);
    }else{
        // 处理同名文件问题
        same_file(net_fd, cwd);
    }

    return 0;
}
